package strategy

import (
	"adam2/internal/domain"
	"adam2/internal/quant/impl/buy"
	"adam2/internal/quant/impl/calculate_data"
	"adam2/internal/quant/impl/filter"
	"adam2/internal/quant/impl/log"
	"adam2/internal/quant/impl/prepare_data"
	"adam2/internal/quant/impl/sell"
	"adam2/internal/quant/impl/transaction_date"
	"adam2/internal/quant/impl/update_quant_account"
	_interface "adam2/internal/quant/interface"
	"adam2/internal/quant/step"
	"anubis-framework/pkg/io"
)

// 炸板策略
type PriceLimitBreakdownStrategy struct {
	transactionDateStep              *step.TransactionDateStep
	updateQuantAccountBeforeSellStep *step.UpdateQuantAccountBeforeSellStep
	sellStep                         *step.SellStep
	updateQuantAccountAfterSellStep  *step.UpdateQuantAccountAfterSellStep
	prepareDataStep                  *step.PrepareDataStep
	filterStep                       *step.FilterStep
	buyStep                          *step.BuyStep
	updateQuantAccountAfterBuyStep   *step.UpdateQuantAccountAfterBuyStep
	calculateQuantDataStep           *step.CalculateQuantDataStep
	commonLogStep                    *step.CommonLogStep
	optimizeStep                     *step.OptimizeStep
}

// 初始化
func (p *PriceLimitBreakdownStrategy) init(transactionDateStep *step.TransactionDateStep,
	updateQuantAccountBeforeSellStep *step.UpdateQuantAccountBeforeSellStep, sellStep *step.SellStep,
	updateQuantAccountAfterSellStep *step.UpdateQuantAccountAfterSellStep, prepareDataStep *step.PrepareDataStep,
	filterStep *step.FilterStep, buyStep *step.BuyStep, updateQuantAccountAfterBuyStep *step.UpdateQuantAccountAfterBuyStep,
	calculateQuantDataStep *step.CalculateQuantDataStep, commonLogStep *step.CommonLogStep, optimizeStep *step.OptimizeStep) {
	p.transactionDateStep = transactionDateStep
	p.updateQuantAccountBeforeSellStep = updateQuantAccountBeforeSellStep
	p.sellStep = sellStep
	p.updateQuantAccountAfterSellStep = updateQuantAccountAfterSellStep
	p.prepareDataStep = prepareDataStep
	p.filterStep = filterStep
	p.buyStep = buyStep
	p.updateQuantAccountAfterBuyStep = updateQuantAccountAfterBuyStep
	p.calculateQuantDataStep = calculateQuantDataStep
	p.commonLogStep = commonLogStep
	p.optimizeStep = optimizeStep
}

// 配置策略
func (p *PriceLimitBreakdownStrategy) ConfigStrategy() {
	// 初始化ManyTransactionDate
	var manyTransactionDate _interface.TransactionDate = &transaction_date.ManyTransactionDate{}
	manyTransactionDate.Init(domain.DataSource_.GormDb)
	// 初始化TransactionDateStep
	var transactionDateStep step.TransactionDateStep = step.TransactionDateStep{}
	transactionDateStep.Init(&manyTransactionDate)

	// 初始化HoldTimeSellDate
	var holdTimeSellDate _interface.SellDate = &transaction_date.HoldTimeSellDate{}
	holdTimeSellDate.Init(domain.DataSource_.GormDb)
	// 初始化HoldTimeBuyDate
	var holdTimeBuyDate _interface.BuyDate = &transaction_date.HoldTimeBuyDate{}
	holdTimeBuyDate.Init(domain.DataSource_.GormDb)
	// 初始化HoldTimeStatisticsDate
	var holdTimeStatisticsDate _interface.StatisticsDate = &transaction_date.HoldTimeStatisticsDate{}
	holdTimeStatisticsDate.Init(domain.DataSource_.GormDb)

	// 初始化UpdateQuantAccountBeforeSell
	var updateQuantAccountBeforeSell _interface.UpdateQuantAccount = &update_quant_account.UpdateQuantAccountBeforeSell{}
	updateQuantAccountBeforeSell.Init(domain.DataSource_.GormDb)
	// 初始化UpdateQuantAccountBeforeSellStep
	var updateQuantAccountBeforeSellStep step.UpdateQuantAccountBeforeSellStep = step.UpdateQuantAccountBeforeSellStep{}
	updateQuantAccountBeforeSellStep.Init(&updateQuantAccountBeforeSell, &holdTimeSellDate)

	// 初始化CommonSell
	var commonSell _interface.Sell = &sell.CommonSell{}
	commonSell.Init(domain.DataSource_.GormDb)
	// 初始化CommonDelistSell
	var commonDelistSell _interface.Sell = &sell.CommonDelistSell{}
	commonDelistSell.Init(domain.DataSource_.GormDb)
	// 初始化CommonResolveFailSell
	var commonResolveFailSell _interface.Sell = &sell.CommonResolveFailSell{}
	commonResolveFailSell.Init(domain.DataSource_.GormDb)
	// 初始化SellStep
	var sellStep step.SellStep = step.SellStep{}
	sellStep.Init(&commonSell, &commonDelistSell, &commonResolveFailSell, &holdTimeSellDate)

	// 初始化UpdateQuantAccountAfterSell
	var updateQuantAccountAfterSell _interface.UpdateQuantAccount = &update_quant_account.UpdateQuantAccountAfterSell{}
	updateQuantAccountAfterSell.Init(domain.DataSource_.GormDb)
	// 初始化UpdateQuantAccountAfterSellStep
	var updateQuantAccountAfterSellStep step.UpdateQuantAccountAfterSellStep = step.UpdateQuantAccountAfterSellStep{}
	updateQuantAccountAfterSellStep.Init(&holdTimeSellDate, &updateQuantAccountAfterSell)

	// 初始化PriceLimitBreakdownPrepareData
	var priceLimitBreakdownPrepareData _interface.PrepareData = &prepare_data.PriceLimitBreakdownPrepareData{}
	priceLimitBreakdownPrepareData.Init(domain.DataSource_.GormDb)
	// 初始化PrepareDataStep
	var prepareDataStep step.PrepareDataStep = step.PrepareDataStep{}
	prepareDataStep.Init(&priceLimitBreakdownPrepareData, &holdTimeStatisticsDate)

	// 初始化FilterStep
	var filterStep step.FilterStep = step.FilterStep{}
	filterStep.Init(&holdTimeBuyDate, &holdTimeStatisticsDate)
	// 初始化TurnoverSurgeFilter
	var turnoverSurgeFilter _interface.Filter = &filter.TurnoverSurgeFilter{}
	turnoverSurgeFilter.Init(domain.DataSource_.GormDb)
	filterStep.AddStatisticsDateFilter(&turnoverSurgeFilter)
	// 初始化BearishFilter
	var bearishFilter _interface.Filter = &filter.BearishFilter{}
	bearishFilter.Init(domain.DataSource_.GormDb)
	filterStep.AddStatisticsDateFilter(&bearishFilter)
	// 初始化StAndDelistedFilter
	//var stAdnDelistedFilter _interface.Filter = &filter.StAdnDelistFilter{}
	//stAdnDelistedFilter.Init(domain.DataSource_.GormDb)
	//filterStep.AddStatisticsDateFilter(&stAdnDelistedFilter)
	// 初始化UpperShadowPercentageLimitFilter
	var upperShadowPercentageLimitFilter _interface.Filter = &filter.UpperShadowPercentageLimitFilter{}
	upperShadowPercentageLimitFilter.Init(domain.DataSource_.GormDb)
	filterStep.AddStatisticsDateFilter(&upperShadowPercentageLimitFilter)
	// 初始化OneWordLimitUpFilter
	var oneWordLimitUpFilter _interface.Filter = &filter.OneWordLimitUpFilter{}
	oneWordLimitUpFilter.Init(domain.DataSource_.GormDb)
	filterStep.AddBuyDateFilter(&oneWordLimitUpFilter)
	// 初始化UpBeyondThresholdWithinNDateFilter
	var upBeyondThresholdWithinNDateFilter _interface.Filter = &filter.UpBeyondThresholdWithinNDateFilter{}
	upBeyondThresholdWithinNDateFilter.Init(domain.DataSource_.GormDb)
	filterStep.AddBuyDateFilter(&upBeyondThresholdWithinNDateFilter)

	// 初始化CommonBuy
	var commonBuy _interface.Buy = &buy.CommonBuy{}
	commonBuy.Init(domain.DataSource_.GormDb)
	// 初始化BuyStep
	var buyStep step.BuyStep = step.BuyStep{}
	buyStep.Init(&commonBuy, &holdTimeBuyDate)

	// 初始化UpdateQuantAccountAfterBuy
	var updateQuantAccountAfterBuy _interface.UpdateQuantAccount = &update_quant_account.UpdateQuantAccountAfterBuy{}
	updateQuantAccountAfterBuy.Init(domain.DataSource_.GormDb)
	// 初始化UpdateQuantAccountAfterBuyStep
	var updateQuantAccountAfterBuyStep step.UpdateQuantAccountAfterBuyStep = step.UpdateQuantAccountAfterBuyStep{}
	updateQuantAccountAfterBuyStep.Init(&holdTimeBuyDate, &updateQuantAccountAfterBuy)

	// 初始化CalculateQuantMaData
	var calculateQuantMaData _interface.CalculateData = &calculate_data.CalculateQuantMaData{}
	calculateQuantMaData.Init(domain.DataSource_.GormDb)
	// 初始化CalculateQuantBiasData
	var calculateQuantBiasData _interface.CalculateData = &calculate_data.CalculateQuantBiasData{}
	calculateQuantBiasData.Init(domain.DataSource_.GormDb)
	// 初始化
	var calculateDataStep step.CalculateQuantDataStep = step.CalculateQuantDataStep{}
	calculateDataStep.Init(&calculateQuantMaData, &calculateQuantBiasData)

	// 初始化CommonLog
	var commonLog _interface.Log = &log.CommonLog{}
	commonLog.Init(domain.DataSource_.GormDb)
	// 初始化CommonLogStep
	var commonLogStep step.CommonLogStep = step.CommonLogStep{}
	commonLogStep.Init(&commonLog)

	// 初始化PriceLimitBreakdownStrategy
	p.init(&transactionDateStep, &updateQuantAccountBeforeSellStep, &sellStep, &updateQuantAccountAfterSellStep,
		&prepareDataStep, &filterStep, &buyStep, &updateQuantAccountAfterBuyStep, &calculateDataStep, &commonLogStep,
		nil)
}

// 开始
func (p *PriceLimitBreakdownStrategy) Start() {
	// 查询交易日期
	var transactionDateArray []string = p.transactionDateStep.Exec()
	if transactionDateArray != nil && len(transactionDateArray) > 0 {
		for _, transactionDate := range transactionDateArray {
			if transactionDate == "2025-11-20" {
				io.Infoln("")
			}

			// 每日---卖出之前更新quant_account
			p.updateQuantAccountBeforeSellStep.UpdateQuantAccountBeforeSellStep(transactionDate)

			// 统计日---准备数据
			p.prepareDataStep.Exec(transactionDate)

			// 统计日、买入日---过滤股票
			p.filterStep.Exec(transactionDate)

			// 每日、卖出日---卖出
			p.sellStep.Exec(transactionDate)

			// 卖出日---在卖出之后更新quant_account
			p.updateQuantAccountAfterSellStep.UpdateQuantAccountAfterSell(transactionDate)

			// 买入日---买入
			p.buyStep.Exec(transactionDate)

			// 买入日---在买入之后更新quant_account表
			p.updateQuantAccountAfterBuyStep.UpdateQuantAccountAfterBuy(transactionDate)

			// 每日---向表quant_account_log中插入记录
			p.commonLogStep.Exec(transactionDate)

			// 每日---计算基础数据
			p.calculateQuantDataStep.Exec(transactionDate)

			// 优化
			//p.optimizeStep.Exec(transactionDate)
		}
	} else {
		io.Infoln("交易日期为空，程序结束")
	}
}
